<?php
if($_SESSION['login'] != true) {
	die('Access danied');
}
//------------------------------
define(ACTION_ACTIVATE,		'actionActivate');
define(ACTION_DEACTIVATE, 	'actionDeactivate');
define(ACTION_ADD,			'actionAdd');
//------------------------------
//$scriptAddPortalNumber = 0;
//------------------------------
if(IsSet($_GET['action'])) {
	$action = $_GET['action'];
	
	if($action == ACTION_ADD) {
		$searcherName = $_POST['searcherName'];
		$searcherAddr = $_POST['searcherAddr'];
		$searcherIcon = $_POST['searcherIcon'];
		addSeracher($searcherName, $searcherAddr, $searcherIcon);
	} else if(IsSet($_GET['searcherId'])) {
		$searcherId = $_GET['searcherId'];
		
		if($action == ACTION_ACTIVATE) {
			activateSearcher($searcherId, 1);
		} else if($action == ACTION_DEACTIVATE) {
			activateSearcher($searcherId, 0);
		}
	}
}

//------------------------------
$db->query("SELECT searcher_id, name, url, active, icon FROM \"" . $configDbPrefix . "searchers\" ORDER BY name;");
$rows	= $db->fetchArray();

insertMenu(0);
addSearcherForm(0);
?>
<br>
<table>
	<tr class="bgWhite">
		<th></td>
		<th></td>
		<th>Nazwa</th>
		<th>Adres</th>
		<th></th>
		<!--<th colspan="1" style="text-align: center;">Opcje</th>-->
	</tr>
	<?php
	$i = 0;
	foreach($rows as $row) {
		$i++;
		$searcherId = $row['searcher_id'];
		if($i % 2 == 0) {
			?>
			<tr class="bgWhite">
			<?php
		} else {
			?>
			<tr class="bgGray">
			<?php
		}
		?>
			<!-- kolejny numer -->
			<td width="5%"><?php echo $i . '.' ?></td>
			<!-- przycisk ON/OFF -->
			<td width="3%">
				<?php
					$link = $PHP_SELF . '?configType=' . MANAGE_SEARCHERS  . '&searcherId=' . $searcherId;
					if($row['active']) {
						$link .= '&action=' . ACTION_DEACTIVATE;
						?>
						<a href="<?php echo $link ?>"><img src="images/onoff.png" alt="Wyłącz" title="Wyłącz"></a>
						<?php
					} else {
						$link .= '&action=' . ACTION_ACTIVATE;
						?>
						<a href="<?php echo $link ?>"><img src="images/onoff.png" alt="Włącz" title="Włącz"></a>
						<?php
					}
				?>
			</td>
			<!-- nazwa -->
			<td>
				<?php
					if($row['active']) {
						?>
							<div class="activeOn">
								<?php echo $row['name']; ?>
							</div>
						<?php
					} else {
						?>
							<div class="activeOff">
								<?php echo $row['name']; ?>
							</div>
						<?php
					}
				?>
			</td>
			<!-- adres -->
			<td>
				<?php echo $row['url']; ?>
			</td>
			<!-- ikonka -->
			<td>
				<img src="./images/searchers/<?php echo $row['icon']; ?>" />
			</td>
		</tr>
		<?php
	}
	?>
</table>
<br>
<?php
addSearcherForm(1);
insertMenu(1);
//enableConfirm();

/*
*	FUNKCJE
*/

function insertAddNewPortal($idN) {
	$link = $PHP_SELF . '?configType=' . MONITOR_AND_MANAGE . '&action=' . ACTION_ADD;
	?>
	<form method="POST" action="<?php echo $link ?>" id="addNewPortal<?php echo $idN ?>" style="display:none;">
		Nazwa portalu: <input type="text" name="portalName">&nbsp;
		Adres portalu: <input type="text" name="portalUrl" value="http://">&nbsp;
		<button type="submit"><img src="images/add.gif">&nbsp;Dodaj nowy portal</button>
	</form>
	<?php
}

function insertMenu($idN) {
	?>
	<script type="text/javascript">
		$(document).ready(
			function(){
				$("#addNewSearcherLink<?php echo $idN ?>").click(
					function(){
						$("#addNewSearcher<?php echo $idN ?>").toggle("normal");
					}
				);
			}
		);
	</script>
	<script type="text/javascript">
		$(document).ready(
			function(){
				$("#appStateLink<?php echo $idN ?>").click(
					function(){
						$("#appState<?php echo $idN ?>").toggle("normal");
					}
				);
			}
		);
	</script>
	<a href="../"><img src="images/previous.png " alt="Wstecz">&nbsp;Strona główna</a>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="#" id="addNewSearcherLink<?php echo $idN ?>" ><img src="images/add.gif" alt="Dodaj wyszukiwarkę">&nbsp;Dodaj wyszukiwarkę</a>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<!--<a href="#" id="appStateLink<?php echo $idN ?>" ><img src="images/action.gif" alt="Dodaj nowy portal">&nbsp;Stan aplikacji</a>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="<?php echo $PHP_SELF . '?configType=' . MONITOR_AND_MANAGE . '&actionType=' . ACTION_TYPE_STATISTICS ?>"><img src="images/statistics.gif" alt="Statystyki">&nbsp;Statystyki ogólne</a>-->
	<br>
	<br>
	<?php
}

function activateSearcher($searcherId, $value) {
	global $db;
	global $configDbPrefix;
	$db->query("UPDATE \"" . $configDbPrefix . "searchers\" SET active=$value WHERE searcher_id=$searcherId;");
}

function addSearcherForm($idN) {
	$link = $PHP_SELF . '?configType=manageSearchers&action=' . ACTION_ADD;
	?>
	<form method="POST" action="<?php echo $link; ?>" id="addNewSearcher<?php echo $idN ?>" style="display:none;">
		Nazwa:&nbsp;<input type="text" name="searcherName"><br />
		Adres:&nbsp;<input type="text" name="searcherAddr" value="http://"><br />
		Ikona:&nbsp;<input type="text" name="searcherIcon" value=".png"><br />
		<button type="submit"><img src="images/save.png">&nbsp;Zapisz</button>
	</form>
	<?php
}

function addSeracher($searcherName, $searcherAddr, $searcherIcon) {
	global $db;
	global $configDbPrefix;
	$db->query("INSERT INTO \"" . $configDbPrefix . "searchers\"(name, url, active, icon) VALUES('$searcherName', '$searcherAddr', 0, '$searcherIcon');");
}
?>
